import 'package:flutter/material.dart';
import '../repositories/mood_repository.dart';
import '../core/api_logger.dart';

enum MoodType {
  sunny('☀️', '晴朗'),
  cloudy('☁️', '多云'),
  rainy('🌧️', '下雨'),
  snowy('❄️', '下雪'),
  happy('😊', '开心'),
  heart('❤️', '开心'),
  love('🥰', '恋爱'),
  sad('😢', '难过'),
  thoughtful('🤔', '思考'),
  grateful('🙏', '感恩');

  const MoodType(this.emoji, this.description);
  final String emoji;
  final String description;
}

class MoodProvider extends ChangeNotifier {
  final MoodRepository _repository;
  MoodType? _myMood;
  MoodType? _partnerMood;
  DateTime _lastMoodUpdate = DateTime.now();
  bool _isLoading = false;
  String? _error;

  MoodType? get myMood => _myMood;
  MoodType? get partnerMood => _partnerMood;
  DateTime get lastMoodUpdate => _lastMoodUpdate;
  bool get isLoading => _isLoading;
  String? get error => _error;

  // 根据双方心情生成背景色
  Color get backgroundColorFromMood {
    if (_myMood == null || _partnerMood == null) {
      return const Color(0xFFF8F9FA);
    }

    // 根据心情组合返回不同的背景色
    if (_myMood == MoodType.heart && _partnerMood == MoodType.heart) {
      return const Color(0xFFFFE4E6); // 粉红色
    } else if (_myMood == MoodType.sunny && _partnerMood == MoodType.sunny) {
      return const Color(0xFFFFF3CD); // 阳光黄
    } else if (_myMood == MoodType.cloudy || _partnerMood == MoodType.cloudy) {
      return const Color(0xFFE2E3E5); // 灰色
    } else if (_myMood == MoodType.rainy || _partnerMood == MoodType.rainy) {
      return const Color(0xFFD1ECF1); // 蓝灰色
    } else {
      return const Color(0xFFF8F9FA); // 默认色
    }
  }

  MoodProvider(this._repository) {
    _loadMoodData();
  }

  Future<void> _loadMoodData() async {
    _setLoading(true);
    
    try {
      final myMoodResponse = await _repository.getMyMood();
      if (myMoodResponse.isSuccess) {
        _myMood = myMoodResponse.data;
      }
      
      final partnerMoodResponse = await _repository.getPartnerMood();
      if (partnerMoodResponse.isSuccess) {
        _partnerMood = partnerMoodResponse.data;
      }
      
      final lastUpdateResponse = await _repository.getLastMoodUpdate();
      if (lastUpdateResponse.isSuccess && lastUpdateResponse.data != null) {
        _lastMoodUpdate = lastUpdateResponse.data!;
      }
      
      _clearError();
    } catch (e) {
      _setError('加载心情数据失败: $e');
    } finally {
      _setLoading(false);
    }
  }

  Future<void> setMyMood(MoodType mood) async {
    _setLoading(true);
    
    try {
      final response = await _repository.setMyMood(mood);
      if (response.isSuccess) {
        _myMood = mood;
        _lastMoodUpdate = DateTime.now();
        _clearError();
      } else {
        _setError(response.error ?? '设置我的心情失败');
      }
    } catch (e) {
      _setError('设置我的心情失败: $e');
    } finally {
      _setLoading(false);
    }
  }

  Future<void> setPartnerMood(MoodType mood) async {
    _setLoading(true);
    
    try {
      final response = await _repository.setPartnerMood(mood);
      if (response.isSuccess) {
        _partnerMood = mood;
        _lastMoodUpdate = DateTime.now();
        _clearError();
      } else {
        _setError(response.error ?? '设置伴侣心情失败');
      }
    } catch (e) {
      _setError('设置伴侣心情失败: $e');
    } finally {
      _setLoading(false);
    }
  }

  // 获取每日情话
  Future<String> getDailyLoveQuote() async {
    ApiLogger.logProvider('MOOD_PROVIDER', '请求每日情话');
    try {
      final response = await _repository.getDailyLoveQuote();
      final content = response.data?.content ?? '今天也要开心哦 😊';
      ApiLogger.logProvider('MOOD_PROVIDER', '获取情话成功', data: '"$content"');
      return content;
    } catch (e) {
      ApiLogger.logProvider('MOOD_PROVIDER', '获取情话失败，使用默认内容', data: e.toString());
      return '今天也要开心哦 😊';
    }
  }
  
  void _setLoading(bool loading) {
    _isLoading = loading;
    notifyListeners();
  }
  
  void _setError(String error) {
    _error = error;
    notifyListeners();
  }
  
  void _clearError() {
    _error = null;
    notifyListeners();
  }
}